<?



include("config.php");
$already = 0;

// On verifie si l'inscription à ce stream n'a pas déjà été effectué
$requete = "SELECT * FROM abonnement WHERE user_v='" . $_SESSION['pseudo'] . "' AND id_user='" . $_GET['user'] . "'";
$result = mysql_query ($requete);
if(mysql_num_rows($result) != 0)
{
			$already = 1;
}



	// On verifie si l'utilisateur est connecté et si un id de stream est spécidié
	if(isset($_GET['action']))
	{
		// On se connecte à la base de donnée
		$link = mysql_connect ('localhost', DB_USER_NAME, DB_PASSWORD) or die ("Cannot join database");
		mysql_select_db (DB_NAME);

		if($_GET['action'] == "s")
		{
			$requete = "SELECT * FROM user WHERE pseudo='" . $_GET['user'] . "'";
			$result = mysql_query ($requete);
			if(mysql_num_rows($result) != 0)
			{
				$requete = "INSERT INTO abonnement(user_v,id_user) 
									VALUES ('" . $_SESSION['pseudo'] . "', '" . $_GET['user'] . "')";
				mysql_query ($requete);
				echo "Subscription to the stream succeded<br>";
				$already = 1;
			}
			else
			{
				echo "User doesn't exist";
			}
		}
		else if($_GET['action'] == "u")
		{
			$requete = "DELETE FROM abonnement WHERE user_v ='" . $_SESSION['pseudo'] . "' AND id_user='" . $_GET['user'] . "'";
			$result = mysql_query ($requete);
			echo '<meta http-equiv="refresh" content="0; URL=index.php?user=' . $_GET['user'] . '">';

		}
		else if($_GET['action'] == "d")
		{
			$requete = "DELETE FROM user WHERE pseudo ='" . $_GET['user'] . "'";
			$result = mysql_query ($requete);

			$requete = "DELETE FROM stream WHERE user ='" . $_GET['user'] . "'";
			$result = mysql_query ($requete);

			$requete = "DELETE FROM abonnement WHERE user_v ='" . $_GET['user'] . "'";
			$result = mysql_query ($requete);
		}
	


	}


if($already == 0)
{
	if($_SESSION['pseudo'] != $_GET['user'])
	{
		echo "<a href=\"index.php?user=" . $_GET['user'] . "&action=s\"> Subscribe to this member streams </a><br>";
	}
}
else
{
	echo "<a href=\"index.php?user=" . $_GET['user'] . "&action=u\"> Unsubscribe to this member streams </a><br>";
}

if($_SESSION['pseudo'] == "admin")
{
	echo "<a href=\"index.php?user=" . $_GET['user'] . "&action=d\"> Delete this member </a><br>";
}
?>

